/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.command;

import java.io.File;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ListIterator;

import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.view.ExtensionFileFilter;
import br.ufmg.dees.insane.crossSection.CrossSection;
import br.ufmg.dees.insane.drivers.Driver;
import br.ufmg.dees.insane.drivers.StructuralMech;
import br.ufmg.dees.insane.materialMedia.material.LinearElasticIsotropic;
import br.ufmg.dees.insane.materialMedia.material.MSpring;
import br.ufmg.dees.insane.materialMedia.material.Material;
import br.ufmg.dees.insane.model.disc.analysismodel.AnalysisModel;
import br.ufmg.dees.insane.model.disc.element.Element;
import br.ufmg.dees.insane.model.disc.element.ElmAxiQ4;
import br.ufmg.dees.insane.model.disc.element.ElmAxiQ8;
import br.ufmg.dees.insane.model.disc.element.ElmAxiQ9;
import br.ufmg.dees.insane.model.disc.element.ElmH20;
import br.ufmg.dees.insane.model.disc.element.ElmH8;
import br.ufmg.dees.insane.model.disc.element.ElmL2;
import br.ufmg.dees.insane.model.disc.element.ElmL3;
import br.ufmg.dees.insane.model.disc.element.ElmL4;
import br.ufmg.dees.insane.model.disc.element.ElmQ4;
import br.ufmg.dees.insane.model.disc.element.ElmQ8;
import br.ufmg.dees.insane.model.disc.element.ElmQ9;
import br.ufmg.dees.insane.model.disc.element.ElmT10;
import br.ufmg.dees.insane.model.disc.element.ElmT3;
import br.ufmg.dees.insane.model.disc.element.ElmT6;
import br.ufmg.dees.insane.model.disc.element.ElmTetra4;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.model.disc.element.ParametricElement;
import br.ufmg.dees.insane.model.disc.element.force.ElementForce;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.integrationpoint.IntegrationPoint;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.model.disc.pointforce.PointForce;
import br.ufmg.dees.insane.model.disc.shape.Shape;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;
import br.ufmg.dees.insane.util.IMatrix;
import br.ufmg.dees.insane.util.IVector;


/**
 * This class is an implementation of the interface Command.
 * its unabled to undo or redo.
 * its "execute" method encodes the current fem model as
 * a XML file in a given file.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since February 2004
 * @see gui.command.Command
 */
public class ExportXMLCommand implements Command {
    
    private InsaneInternalInterface parent;
    private JFileChooser fileChooser = new JFileChooser();
    private ExtensionFileFilter filter = new ExtensionFileFilter(new String[]{"xml"}, "XML Files");
	private Driver driver;
	private Object docOutput;
	private boolean processed = false;
	private boolean hypostatic = false;
	private boolean validFile;
	
	private static int ELM_FRAME = 1;
	private static int ELM_PARAMETRIC = 2;
	
//*****************************************************************************
	
    /**
     * Creates new ExportXMLCommand
     * and sets the FileFilter of FileChooser.
     *
     * @param parent The JInternalFrame parent.
     */
    public ExportXMLCommand(JInternalFrame parent) {
        this.parent = (InsaneInternalInterface)parent;
        fileChooser.setFileFilter(filter);
    }
    
//*****************************************************************************
	
    /**
     * Calls the file chooser an encode the fem model as a XML file.
     *
     * @see gui.command.Command#execute()
     */
    public void execute() {
		
		validFile = false;
		while (!validFile) {
			
			fileChooser.setDialogTitle("Export XML");
			int retVal = fileChooser.showDialog(parent, "Export");
			if (retVal == JFileChooser.APPROVE_OPTION) {
				
				//Getting the file name
				String s = fileChooser.getSelectedFile().getName();
				
				//Getting the file extension
				String ext = new String();
				int i = s.lastIndexOf('.');
				if (i > 0 &&  i < s.length() - 1) {
					ext = s.substring(i+1).toLowerCase();
				}
				
				//Comparing the extension
				if (!ext.equals("xml")) {
					s = s + ".xml";
					File file = fileChooser.getSelectedFile();
					
					//Changing the file in case of no good extensions
					fileChooser.setSelectedFile(new File(file.getParent() + File.separator + s));
				}
				
				//Checks if the file exists
				if (fileChooser.getSelectedFile().exists()){
						int value = 10;
						value = JOptionPane.showConfirmDialog(parent,
						"File \"" + fileChooser.getSelectedFile().getPath() + "\" exists.\nDo you want to replace it?",
						"Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
						if (value == JOptionPane.YES_OPTION){
							validFile = true;
						}
				}
				else {
					validFile = true;
				}
			}
			else return;
		}
		
		try {
			driver = (StructuralMech)this.parent.getModel().getDriver();
			if (driver.getFemModel().getNumberOfEquations() != 0 && driver.getFemModel().getNumberOfRestraints() != 0) {processed = true;}
			if (driver.getFemModel().isHypostatic()) {hypostatic = true;}
			buildDOMTreeFromDriver();
			serializeXMLDocument(fileChooser.getSelectedFile());
		} catch (NullPointerException e) {
			parent.addOutputText("ERROR - Could not export XML file.");
		} catch (Exception e) {
			parent.addOutputText("ERROR - Could not export XML file.");
		}
		validFile = false;
	}
    
//*****************************************************************************
	
    /**
     * Impossible to undo - not implemented.
     */
    public void undo() {}
    
//*****************************************************************************
	
    /**
     * Impossible to redo - not implemented.
     */
    public void redo() {}
    
//*****************************************************************************
	
	/** Builds the DOM tree from the DiscreteModel.*/
	public void buildDOMTreeFromDriver() {
		
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document doc = builder.newDocument();
			
			//Creating formator
			DecimalFormat fmt = new DecimalFormat();
			DecimalFormatSymbols fsym = new DecimalFormatSymbols();
			fsym.setDecimalSeparator('.');
			fmt.setDecimalFormatSymbols(fsym);
			
			//All the objects which will be serialized are referenced here
//			Solution solution = driver.getSolution();
			FemModel model = driver.getFemModel();
			Node node;
			Material mat;
			LinearElasticIsotropic matIso;
			MSpring matSpr;
			CrossSection cs;
			Element elm;
			AnalysisModel anl;
			PointForce pf;
			ElementForce ef;
			IntegrationPoint ip;
			int elmType = 0;
			
			//Creating DOM element "Driver"
			org.w3c.dom.Element elmDriver = doc.createElement("Driver");
			if (driver instanceof StructuralMech)
				elmDriver.setAttribute("class", "StructuralMech");
			doc.appendChild(elmDriver);
			
			//Creating DOM element "Solution"
			//org.w3c.dom.Element elmSolution = doc.createElement("Solution");
			/*if (solution instanceof OnePointEq)
				elmSolution.setAttribute("class", "OnePointEq");
			elmDriver.appendChild(elmSolution);*/
			
			//Creating DOM element "Model"
			org.w3c.dom.Element elmModel = doc.createElement("Model");
			elmDriver.appendChild(elmModel);
			
			//Creating DOM element "NumberOfEquations"
			if (processed) {
				org.w3c.dom.Element elmNumEquations = doc.createElement("NumberOfEquations");
				elmNumEquations.appendChild(doc.createTextNode(String.valueOf(model.getNumberOfEquations())));
				elmModel.appendChild(elmNumEquations);
			}
			
			//Creating DOM element "NumberOfRestraints"
			if (processed) {
				org.w3c.dom.Element elmNumRestraints = doc.createElement("NumberOfRestraints");
				elmNumRestraints.appendChild(doc.createTextNode(String.valueOf(model.getNumberOfRestraints())));
				elmModel.appendChild(elmNumRestraints);
			}
			
			//Creating DOM element "NodeList"
			org.w3c.dom.Element elmNodeList = doc.createElement("NodeList");
			elmModel.appendChild(elmNodeList);
			
			//Creating DOM element "MaterialList"
			org.w3c.dom.Element elmMaterialList = doc.createElement("MaterialList");
			elmModel.appendChild(elmMaterialList);
			
			//Creating DOM element "CrossSectionList"
			org.w3c.dom.Element elmCrossSectionList = doc.createElement("CrossSectionList");
			elmModel.appendChild(elmCrossSectionList);
			
			//Creating DOM element "ElementList"
			org.w3c.dom.Element elmElementList = doc.createElement("ElementList");
			elmModel.appendChild(elmElementList);
			
			//Creating DOM element "GlobalAnalysisModel"
			try {
				org.w3c.dom.Element elmGlobalAnalysisModel = doc.createElement("GlobalAnalysisModel");
				elmGlobalAnalysisModel.appendChild(doc.createTextNode(String.valueOf(model.getGlobalAnalysisModel().getType())));
				elmModel.appendChild(elmGlobalAnalysisModel);
			} catch (Exception e) {}
			
			//Creating DOM elements "Node"
			ListIterator nodes = model.getNodesList().listIterator();
			while (nodes.hasNext()) {
				node = (Node)nodes.next();
				org.w3c.dom.Element elmNode = doc.createElement("Node");
				elmNode.setAttribute("label", "" + node.getLabel());
				elmNodeList.appendChild(elmNode);
				
				//Creating DOM elements "Coord"
				org.w3c.dom.Element elmCoord = doc.createElement("Coord");
				elmCoord.appendChild(doc.createTextNode(String.valueOf(node.getCoord().x)));
				elmCoord.appendChild(doc.createTextNode(" "));
				elmCoord.appendChild(doc.createTextNode(String.valueOf(node.getCoord().y)));
				elmCoord.appendChild(doc.createTextNode(" "));
				elmCoord.appendChild(doc.createTextNode(String.valueOf(node.getCoord().z)));
				elmNode.appendChild(elmCoord);
				
				//Creating DOM elements "Restraints"
				org.w3c.dom.Element elmRestraints = doc.createElement("Restraints");
				elmRestraints.appendChild(doc.createTextNode(String.valueOf(node.getRestraint(0))));
				elmRestraints.appendChild(doc.createTextNode(" "));
				elmRestraints.appendChild(doc.createTextNode(String.valueOf(node.getRestraint(1))));
				elmRestraints.appendChild(doc.createTextNode(" "));
				elmRestraints.appendChild(doc.createTextNode(String.valueOf(node.getRestraint(2))));
				elmRestraints.appendChild(doc.createTextNode(" "));
				elmRestraints.appendChild(doc.createTextNode(String.valueOf(node.getRestraint(3))));
				elmRestraints.appendChild(doc.createTextNode(" "));
				elmRestraints.appendChild(doc.createTextNode(String.valueOf(node.getRestraint(4))));
				elmRestraints.appendChild(doc.createTextNode(" "));
				elmRestraints.appendChild(doc.createTextNode(String.valueOf(node.getRestraint(5))));
				elmNode.appendChild(elmRestraints);
				
				//Creating DOM elements "Displacements"
				if (processed && !hypostatic) {
					org.w3c.dom.Element elmDisplacements = doc.createElement("Displacements");
					fmt.applyPattern("0.000E00");
					elmDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getDisplacement(0)))));
					elmDisplacements.appendChild(doc.createTextNode(" "));
					elmDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getDisplacement(1)))));
					elmDisplacements.appendChild(doc.createTextNode(" "));
					elmDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getDisplacement(2)))));
					elmDisplacements.appendChild(doc.createTextNode(" "));
					elmDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getDisplacement(3)))));
					elmDisplacements.appendChild(doc.createTextNode(" "));
					elmDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getDisplacement(4)))));
					elmDisplacements.appendChild(doc.createTextNode(" "));
					elmDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getDisplacement(5)))));
					elmNode.appendChild(elmDisplacements);
				}
				
				//Creating DOM elements "Load"
				org.w3c.dom.Element elmLoad = doc.createElement("Load");
				fmt.applyPattern("####0.000");
				elmLoad.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getForce(0)))));
				elmLoad.appendChild(doc.createTextNode(" "));
				elmLoad.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getForce(1)))));
				elmLoad.appendChild(doc.createTextNode(" "));
				elmLoad.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getForce(2)))));
				elmLoad.appendChild(doc.createTextNode(" "));
				elmLoad.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getForce(3)))));
				elmLoad.appendChild(doc.createTextNode(" "));
				elmLoad.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getForce(4)))));
				elmLoad.appendChild(doc.createTextNode(" "));
				elmLoad.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getForce(5)))));
				elmNode.appendChild(elmLoad);
				
				//Creating DOM elements "PreDisplacement"
				org.w3c.dom.Element elmPreDisplacements = doc.createElement("PreDisplacements");
				fmt.applyPattern("0.000E00");
				elmPreDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getPreDisplacement(0)))));
				elmPreDisplacements.appendChild(doc.createTextNode(" "));
				elmPreDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getPreDisplacement(1)))));
				elmPreDisplacements.appendChild(doc.createTextNode(" "));
				elmPreDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getPreDisplacement(2)))));
				elmPreDisplacements.appendChild(doc.createTextNode(" "));
				elmPreDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getPreDisplacement(3)))));
				elmPreDisplacements.appendChild(doc.createTextNode(" "));
				elmPreDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getPreDisplacement(4)))));
				elmPreDisplacements.appendChild(doc.createTextNode(" "));
				elmPreDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getPreDisplacement(5)))));
				elmPreDisplacements.appendChild(doc.createTextNode(" "));
				elmNode.appendChild(elmPreDisplacements);
				
				//Creating DOM element "mySpring"
				org.w3c.dom.Element elmMySpring = doc.createElement("mySpring");
				fmt.applyPattern("0.000E00");
				elmMySpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getSpring(0)))));
				elmMySpring.appendChild(doc.createTextNode(" "));
				elmMySpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getSpring(1)))));
				elmMySpring.appendChild(doc.createTextNode(" "));
				elmMySpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getSpring(2)))));
				elmMySpring.appendChild(doc.createTextNode(" "));
				elmMySpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getSpring(3)))));
				elmMySpring.appendChild(doc.createTextNode(" "));
				elmMySpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getSpring(4)))));
				elmMySpring.appendChild(doc.createTextNode(" "));
				elmMySpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getSpring(5)))));
				elmMySpring.appendChild(doc.createTextNode(" "));
				elmNode.appendChild(elmMySpring);
				
				//Creating DOM element "Angle"
				org.w3c.dom.Element elmAngle = doc.createElement("Angle");
				elmAngle.appendChild(doc.createTextNode(String.valueOf(node.getAngle())));
				elmNode.appendChild(elmAngle);
				
				//Creating DOM element "ForceOnSpring"
				if (processed && !hypostatic) {
					org.w3c.dom.Element elmForceOnSpring = doc.createElement("ForceOnSpring");
					fmt.applyPattern("####0.000");
					elmForceOnSpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(-node.getDisplacement(0)*node.getSpring(0)))));
					elmForceOnSpring.appendChild(doc.createTextNode(" "));
					elmForceOnSpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(-node.getDisplacement(1)*node.getSpring(1)))));
					elmForceOnSpring.appendChild(doc.createTextNode(" "));
					elmForceOnSpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(-node.getDisplacement(2)*node.getSpring(2)))));
					elmForceOnSpring.appendChild(doc.createTextNode(" "));
					elmForceOnSpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(-node.getDisplacement(3)*node.getSpring(3)))));
					elmForceOnSpring.appendChild(doc.createTextNode(" "));
					elmForceOnSpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(-node.getDisplacement(4)*node.getSpring(4)))));
					elmForceOnSpring.appendChild(doc.createTextNode(" "));
					elmForceOnSpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(-node.getDisplacement(5)*node.getSpring(5)))));
					elmNode.appendChild(elmForceOnSpring);
				}
				
				//Creating DOM element "Reactions"
				if (processed && !hypostatic) {
					org.w3c.dom.Element elmReactions = doc.createElement("Reactions");
					fmt.applyPattern("####0.000");
					elmReactions.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getReaction(0)))));
					elmReactions.appendChild(doc.createTextNode(" "));
					elmReactions.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getReaction(1)))));
					elmReactions.appendChild(doc.createTextNode(" "));
					elmReactions.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getReaction(2)))));
					elmReactions.appendChild(doc.createTextNode(" "));
					elmReactions.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getReaction(3)))));
					elmReactions.appendChild(doc.createTextNode(" "));
					elmReactions.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getReaction(4)))));
					elmReactions.appendChild(doc.createTextNode(" "));
					elmReactions.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getReaction(5)))));
					elmNode.appendChild(elmReactions);
				}
				
			}//End of creating DOM element "Node"
			
			//Creating DOM elements "Material"
			ListIterator mats = model.getMaterialsList().listIterator();
			while (mats.hasNext()) {
				mat = (Material) mats.next();
				org.w3c.dom.Element elmMaterial = doc.createElement("Material");
				if (mat instanceof LinearElasticIsotropic)
					elmMaterial.setAttribute("class", "Isotropic");
				if (mat instanceof MSpring)
					elmMaterial.setAttribute("class", "MSpring");
				elmMaterial.setAttribute("label", "" + mat.getLabel());
				elmMaterialList.appendChild(elmMaterial);
				
				//If Isotropic material
				if (mat.getLabel().equals("Isotropic")) {
					matIso = (LinearElasticIsotropic) mat;
					
					//Creating DOM element "Elasticity"
					org.w3c.dom.Element elmElasticity = doc.createElement("Elasticity");
					elmElasticity.appendChild(doc.createTextNode(String.valueOf(matIso.getMaterialValues(matIso.ELASTICITY).doubleValue())));
					if (matIso.getMaterialValues(matIso.ELASTICITY).doubleValue()!=0)
						elmMaterial.appendChild(elmElasticity);
					
					//Creating DOM element "Poisson"
					org.w3c.dom.Element elmPoisson = doc.createElement("Poisson");
					elmPoisson.appendChild(doc.createTextNode(String.valueOf(matIso.getMaterialValues(matIso.POISSON).doubleValue())));
					if (matIso.getMaterialValues(matIso.POISSON).doubleValue()!=0)
						elmMaterial.appendChild(elmPoisson);
					
					//Creating DOM element "ThermalCoeff"
					org.w3c.dom.Element elmThermalCoeff = doc.createElement("ThermalCoeff");
					elmThermalCoeff.appendChild(doc.createTextNode(String.valueOf(matIso.getMaterialValues(matIso.THERMAL_COEFF).doubleValue())));
					if (matIso.getMaterialValues(matIso.THERMAL_COEFF).doubleValue()!=0)
						elmMaterial.appendChild(elmThermalCoeff);
					
					//Creating DOM element "G"
					org.w3c.dom.Element elmG = doc.createElement("G");
					elmG.appendChild(doc.createTextNode(String.valueOf(matIso.getMaterialValues(matIso.SHEAR_MODULUS).doubleValue())));
					if (matIso.getMaterialValues(matIso.SHEAR_MODULUS).doubleValue()!=0)
						elmMaterial.appendChild(elmG);
					
				}
				
				//If MSpring material
				else if (mat.getLabel().equals("MSpring")) {
					matSpr = (MSpring) mat;
					
					//Creating DOM element "Stiffness"
					org.w3c.dom.Element elmStiffness = doc.createElement("Stiffness");
					fmt.applyPattern("0.000E00");
					elmStiffness.appendChild(doc.createTextNode(String.valueOf(fmt.format(matSpr.getStiffness(0)))));
					elmStiffness.appendChild(doc.createTextNode(" "));
					elmStiffness.appendChild(doc.createTextNode(String.valueOf(fmt.format(matSpr.getStiffness(1)))));
					elmStiffness.appendChild(doc.createTextNode(" "));
					elmStiffness.appendChild(doc.createTextNode(String.valueOf(fmt.format(matSpr.getStiffness(2)))));
					elmStiffness.appendChild(doc.createTextNode(" "));
					elmStiffness.appendChild(doc.createTextNode(String.valueOf(fmt.format(matSpr.getStiffness(3)))));
					elmStiffness.appendChild(doc.createTextNode(" "));
					elmStiffness.appendChild(doc.createTextNode(String.valueOf(fmt.format(matSpr.getStiffness(4)))));
					elmStiffness.appendChild(doc.createTextNode(" "));
					elmStiffness.appendChild(doc.createTextNode(String.valueOf(fmt.format(matSpr.getStiffness(5)))));
					elmMaterial.appendChild(elmStiffness);
				}
				
			}//End of creating DOM element "Material"
			
			//Creating DOM elements "CrossSection"
			ListIterator css = model.getSectionsList().listIterator();
			while (css.hasNext()) {
				cs = (CrossSection)css.next();
				org.w3c.dom.Element elmCrossSection = doc.createElement("CrossSection");
				elmCrossSection.setAttribute("label", "" + cs.getLabel());
				elmCrossSectionList.appendChild(elmCrossSection);
				
				//Creating DOM element "Ix"
				org.w3c.dom.Element elmIx = doc.createElement("Ix");
				elmIx.appendChild(doc.createTextNode(String.valueOf(cs.getIx())));
				if (cs.getIx()!=0)
					elmCrossSection.appendChild(elmIx);
				
				//Creating DOM element "Iy"
				org.w3c.dom.Element elmIy = doc.createElement("Iy");
				elmIy.appendChild(doc.createTextNode(String.valueOf(cs.getIy())));
				if (cs.getIy()!=0)
					elmCrossSection.appendChild(elmIy);
				
				//Creating DOM element "Iz"
				org.w3c.dom.Element elmIz = doc.createElement("Iz");
				elmIz.appendChild(doc.createTextNode(String.valueOf(cs.getIz())));
				if (cs.getIz()!=0)
					elmCrossSection.appendChild(elmIz);
				
				//Creating DOM element "Height"
				org.w3c.dom.Element elmHeight = doc.createElement("Height");
				elmHeight.appendChild(doc.createTextNode(String.valueOf(cs.getHeight())));
				if (cs.getHeight()!=0)
					elmCrossSection.appendChild(elmHeight);
				
				//Creating DOM element "Area"
				org.w3c.dom.Element elmArea = doc.createElement("Area");
				elmArea.appendChild(doc.createTextNode(String.valueOf(cs.getArea())));
				if (cs.getArea()!=0)
					elmCrossSection.appendChild(elmArea);
				
				//Creating DOM element "Thickness"
				org.w3c.dom.Element elmThickness = doc.createElement("Thickness");
				elmThickness.appendChild(doc.createTextNode(String.valueOf(cs.getThickness())));
				if (cs.getThickness()!=0)
					elmCrossSection.appendChild(elmThickness);
				
			}//End of creating DOM elements "CrossSection"
			
			//Creating DOM elements "Element"
			ListIterator elms = model.getElementsList().listIterator();
			while (elms.hasNext()) {
				elm = (Element)elms.next();
				org.w3c.dom.Element elmElement = doc.createElement("Element");
				if (elm instanceof FrameElement) {
					elmElement.setAttribute("class", "FrameElement");
					elmType = ELM_FRAME;
				} else if (elm instanceof ElmQ4) {
					elmElement.setAttribute("class", "ParametricElement.Quadrilateral.Q4");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmQ8) {
					elmElement.setAttribute("class", "ParametricElement.Quadrilateral.Q8");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmQ9) {
					elmElement.setAttribute("class", "ParametricElement.Quadrilateral.Q9");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmT3) {
					elmElement.setAttribute("class", "ParametricElement.Triangular.T3");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmT6) {
					elmElement.setAttribute("class", "ParametricElement.Triangular.T6");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmT10) {
					elmElement.setAttribute("class", "ParametricElement.Triangular.T10");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmH8) {
					elmElement.setAttribute("class", "ParametricElement.Hexahedral.H8");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmH20) {
					elmElement.setAttribute("class", "ParametricElement.Hexahedral.H20");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmTetra4) {
					elmElement.setAttribute("class", "ParametricElement.Tetrahedral.Tetra4");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmAxiQ4) {
					elmElement.setAttribute("class", "ParametricElement.Quadrilateral.AxiQ4");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmAxiQ8) {
					elmElement.setAttribute("class", "ParametricElement.Quadrilateral.AxiQ8");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmAxiQ9) {
					elmElement.setAttribute("class", "ParametricElement.Quadrilateral.AxiQ9");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmL2) {
					elmElement.setAttribute("class", "ParametricElement.Bar.L2");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmL3) {
					elmElement.setAttribute("class", "ParametricElement.Bar.L3");
					elmType = ELM_PARAMETRIC;
				} else if (elm instanceof ElmL4) {
					elmElement.setAttribute("class", "ParametricElement.Bar.L4");
					elmType = ELM_PARAMETRIC;
				}
				
				elmElement.setAttribute("label", "" + elm.getLabel());
				elmElementList.appendChild(elmElement);
				
				//Creating DOM element "Incidence"
				org.w3c.dom.Element elmIncidence = doc.createElement("Incidence");
				int i = 0;
				ListIterator elementNodes = elm.getIncidence().listIterator();
				while (elementNodes.hasNext())
				{
					node = (Node)elementNodes.next();
					elmIncidence.appendChild(doc.createTextNode(String.valueOf(elm.getNode(i).getLabel())));
					if(elementNodes.hasNext())
					{
						elmIncidence.appendChild(doc.createTextNode(" "));
					}
					elmElement.appendChild(elmIncidence);
					i++;
				}
				
				//Creating DOM element "IntegrationOrder"
				if (elmType==ELM_PARAMETRIC && ((ParametricElement)elm).getIntegrationOrder()!=null) {
					org.w3c.dom.Element elmIntegOrder = doc.createElement("IntegrationOrder");
					elmIntegOrder.appendChild(doc.createTextNode(String.valueOf(((ParametricElement)elm).getIntegrationOrder().getXiIntegOrder())));
					elmIntegOrder.appendChild(doc.createTextNode(" "));
					elmIntegOrder.appendChild(doc.createTextNode(String.valueOf(((ParametricElement)elm).getIntegrationOrder().getEtaIntegOrder())));
					elmIntegOrder.appendChild(doc.createTextNode(" "));
					elmIntegOrder.appendChild(doc.createTextNode(String.valueOf(((ParametricElement)elm).getIntegrationOrder().getZetaIntegOrder())));
					elmElement.appendChild(elmIntegOrder);
				}
				
				//Creating DOM element "MyMaterial"
				try {
					org.w3c.dom.Element elmMyMaterial = doc.createElement("MyMaterial");
					elmMyMaterial.appendChild(doc.createTextNode(elm.getMaterial().getLabel()));
					elmElement.appendChild(elmMyMaterial);
				}catch(Exception e) {}
				
				//Creating DOM element "MyCrossSection"
				try {
					org.w3c.dom.Element elmMyCrossSection = doc.createElement("MyCrossSection");
					elmMyCrossSection.appendChild(doc.createTextNode(elm.getCrossSection().getLabel()));
					elmElement.appendChild(elmMyCrossSection);
				}catch(Exception e) {}
				
				//Creating DOM element "MyAnalysisModel"
				try {
					org.w3c.dom.Element elmMyAnalysisModel = doc.createElement("MyAnalysisModel");
					elmMyAnalysisModel.appendChild(doc.createTextNode(elm.getAnalysisModel().getType()));
					elmElement.appendChild(elmMyAnalysisModel);
				}catch(Exception e) {}
				
				//Creating DOM element "Liberations"
				if (elmType==ELM_FRAME) {
					org.w3c.dom.Element elmLiberations = doc.createElement("Liberations");
					elmLiberations.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getLiberation(0))));
					elmLiberations.appendChild(doc.createTextNode(" "));
					elmLiberations.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getLiberation(1))));
					elmLiberations.appendChild(doc.createTextNode(" "));
					elmLiberations.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getLiberation(2))));
					elmLiberations.appendChild(doc.createTextNode(" "));
					elmLiberations.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getLiberation(3))));
					elmLiberations.appendChild(doc.createTextNode(" "));
					elmLiberations.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getLiberation(4))));
					elmLiberations.appendChild(doc.createTextNode(" "));
					elmLiberations.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getLiberation(5))));
					elmLiberations.appendChild(doc.createTextNode(" "));
					elmLiberations.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getLiberation(6))));
					elmLiberations.appendChild(doc.createTextNode(" "));
					elmLiberations.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getLiberation(7))));
					elmLiberations.appendChild(doc.createTextNode(" "));
					elmLiberations.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getLiberation(8))));
					elmLiberations.appendChild(doc.createTextNode(" "));
					elmLiberations.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getLiberation(9))));
					elmLiberations.appendChild(doc.createTextNode(" "));
					elmLiberations.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getLiberation(10))));
					elmLiberations.appendChild(doc.createTextNode(" "));
					elmLiberations.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getLiberation(11))));
					elmElement.appendChild(elmLiberations);
				}
				
				//Creating DOM element "DeltaTemp"
				if (elmType==ELM_FRAME) {
					org.w3c.dom.Element elmDeltaTemp = doc.createElement("DeltaTemp");
					elmDeltaTemp.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getDeltaTemp(0))));
					elmDeltaTemp.appendChild(doc.createTextNode(" "));
					elmDeltaTemp.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getDeltaTemp(1))));
					elmDeltaTemp.appendChild(doc.createTextNode(" "));
					elmDeltaTemp.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getDeltaTemp(2))));
					elmDeltaTemp.appendChild(doc.createTextNode(" "));
					elmElement.appendChild(elmDeltaTemp);
				}
				
				//Creating DOM element "ElmPreDisplacement"
				if (elmType==ELM_FRAME) {
					org.w3c.dom.Element elmElmPreDisplacement = doc.createElement("ElmPreDisplacement");
					elmElmPreDisplacement.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getElmPreDisplacement(0))));
					elmElmPreDisplacement.appendChild(doc.createTextNode(" "));
					elmElmPreDisplacement.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getElmPreDisplacement(1))));
					elmElmPreDisplacement.appendChild(doc.createTextNode(" "));
					elmElmPreDisplacement.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getElmPreDisplacement(2))));
					elmElmPreDisplacement.appendChild(doc.createTextNode(" "));
					elmElmPreDisplacement.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getElmPreDisplacement(3))));
					elmElmPreDisplacement.appendChild(doc.createTextNode(" "));
					elmElmPreDisplacement.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getElmPreDisplacement(4))));
					elmElmPreDisplacement.appendChild(doc.createTextNode(" "));
					elmElmPreDisplacement.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getElmPreDisplacement(5))));
					elmElmPreDisplacement.appendChild(doc.createTextNode(" "));
					elmElmPreDisplacement.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getElmPreDisplacement(6))));
					elmElmPreDisplacement.appendChild(doc.createTextNode(" "));
					elmElmPreDisplacement.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getElmPreDisplacement(7))));
					elmElmPreDisplacement.appendChild(doc.createTextNode(" "));
					elmElmPreDisplacement.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getElmPreDisplacement(8))));
					elmElmPreDisplacement.appendChild(doc.createTextNode(" "));
					elmElmPreDisplacement.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getElmPreDisplacement(9))));
					elmElmPreDisplacement.appendChild(doc.createTextNode(" "));
					elmElmPreDisplacement.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getElmPreDisplacement(10))));
					elmElmPreDisplacement.appendChild(doc.createTextNode(" "));
					elmElmPreDisplacement.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getElmPreDisplacement(11))));
					elmElement.appendChild(elmElmPreDisplacement);
				}
				
				//Creating DOM element "InitialNodalForce"
				if (elmType==ELM_FRAME) {
					org.w3c.dom.Element elmInitialNodalForce = doc.createElement("InitialNodalForce");
					elmInitialNodalForce.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getInitialNodalForce(0))));
					elmInitialNodalForce.appendChild(doc.createTextNode(" "));
					elmInitialNodalForce.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getInitialNodalForce(1))));
					elmInitialNodalForce.appendChild(doc.createTextNode(" "));
					elmInitialNodalForce.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getInitialNodalForce(2))));
					elmInitialNodalForce.appendChild(doc.createTextNode(" "));
					elmInitialNodalForce.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getInitialNodalForce(3))));
					elmInitialNodalForce.appendChild(doc.createTextNode(" "));
					elmInitialNodalForce.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getInitialNodalForce(4))));
					elmInitialNodalForce.appendChild(doc.createTextNode(" "));
					elmInitialNodalForce.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getInitialNodalForce(5))));
					elmInitialNodalForce.appendChild(doc.createTextNode(" "));
					elmInitialNodalForce.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getInitialNodalForce(6))));
					elmInitialNodalForce.appendChild(doc.createTextNode(" "));
					elmInitialNodalForce.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getInitialNodalForce(7))));
					elmInitialNodalForce.appendChild(doc.createTextNode(" "));
					elmInitialNodalForce.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getInitialNodalForce(8))));
					elmInitialNodalForce.appendChild(doc.createTextNode(" "));
					elmInitialNodalForce.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getInitialNodalForce(9))));
					elmInitialNodalForce.appendChild(doc.createTextNode(" "));
					elmInitialNodalForce.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getInitialNodalForce(10))));
					elmInitialNodalForce.appendChild(doc.createTextNode(" "));
					elmInitialNodalForce.appendChild(doc.createTextNode(String.valueOf(((FrameElement)elm).getInitialNodalForce(11))));
					elmElement.appendChild(elmInitialNodalForce);
				}
				
				//Creating DOM element "MyElmPointForce"
				org.w3c.dom.Element elmMyElmPointForce = doc.createElement("MyElmPointForce");
				elmElement.appendChild(elmMyElmPointForce);
				
				//Creating DOM elements "ElmPointForce"
				ListIterator pfs = elm.getElmPointForcesList().listIterator();
				while (pfs.hasNext()) {
					pf = (PointForce)pfs.next();
					org.w3c.dom.Element elmPointForce = doc.createElement("ElmPointForce");
					elmMyElmPointForce.appendChild(elmPointForce);
					
					//Creating DOM element "Point"
					org.w3c.dom.Element elmPoint = doc.createElement("Point");
					elmPoint.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().x)));
					elmPoint.appendChild(doc.createTextNode(" "));
					elmPoint.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().y)));
					elmPoint.appendChild(doc.createTextNode(" "));
					elmPoint.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().z)));
					elmPointForce.appendChild(elmPoint);
					
					//Creating DOM element "Force"
					org.w3c.dom.Element elmForce = doc.createElement("Force");
					elmForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(0))));
					elmForce.appendChild(doc.createTextNode(" "));
					elmForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(1))));
					elmForce.appendChild(doc.createTextNode(" "));
					elmForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(2))));
					elmForce.appendChild(doc.createTextNode(" "));
					elmForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(3))));
					elmForce.appendChild(doc.createTextNode(" "));
					elmForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(4))));
					elmForce.appendChild(doc.createTextNode(" "));
					elmForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(5))));
					elmPointForce.appendChild(elmForce);
					
				}//End of creating DOM elements "ElmPointForce"
				
				//Creating DOM element "MyLineForce"
				org.w3c.dom.Element elmMyLineForce = doc.createElement("MyLineForce");
				elmElement.appendChild(elmMyLineForce);
				
				//Creating DOM element "LineElementForce"
				ListIterator efs = elm.getElmLineForcesList().listIterator();
				while (efs.hasNext()) {
					ef = (ElementForce)efs.next();
					org.w3c.dom.Element elmLineElementForce = doc.createElement("LineElementForce");
					elmMyLineForce.appendChild(elmLineElementForce);
					
					pfs = ef.getPointForcesList().listIterator();
					while (pfs.hasNext()) {
						pf = (PointForce) pfs.next();
						
						//Creating DOM element "PointForce"
						org.w3c.dom.Element elmPointForce = doc.createElement("PointForce");
						elmLineElementForce.appendChild(elmPointForce);
						
						//Creating DOM element "PointForceCoords"
						org.w3c.dom.Element elmPointForceCoords = doc.createElement("PointForceCoords");
						elmPointForceCoords.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().x)));
						elmPointForceCoords.appendChild(doc.createTextNode(" "));
						elmPointForceCoords.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().y)));
						elmPointForceCoords.appendChild(doc.createTextNode(" "));
						elmPointForceCoords.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().z)));
						elmPointForce.appendChild(elmPointForceCoords);
						
						//Creating DOM element "PointForceLoad"
						org.w3c.dom.Element elmPointForceLoad = doc.createElement("PointForceLoad");
						elmPointForceLoad.appendChild(doc.createTextNode(String.valueOf(pf.getForce(0))));
						elmPointForceLoad.appendChild(doc.createTextNode(" "));
						elmPointForceLoad.appendChild(doc.createTextNode(String.valueOf(pf.getForce(1))));
						elmPointForceLoad.appendChild(doc.createTextNode(" "));
						elmPointForceLoad.appendChild(doc.createTextNode(String.valueOf(pf.getForce(2))));
						elmPointForceLoad.appendChild(doc.createTextNode(" "));
						elmPointForceLoad.appendChild(doc.createTextNode(String.valueOf(pf.getForce(3))));
						elmPointForceLoad.appendChild(doc.createTextNode(" "));
						elmPointForceLoad.appendChild(doc.createTextNode(String.valueOf(pf.getForce(4))));
						elmPointForceLoad.appendChild(doc.createTextNode(" "));
						elmPointForceLoad.appendChild(doc.createTextNode(String.valueOf(pf.getForce(5))));
						elmPointForce.appendChild(elmPointForceLoad);
					}
				
				}//End of creating DOM element LineElementForce
				
				//Creating DOM element "MySurfaceForce"
				org.w3c.dom.Element elmMySurfaceForce = doc.createElement("MySurfaceForce");
				elmElement.appendChild(elmMySurfaceForce);
				
				//Creating DOM element "SurfaceElementForce"
				efs = elm.getElmSurfaceForcesList().listIterator();
				while (efs.hasNext()) {
					ef = (ElementForce)efs.next();
					org.w3c.dom.Element elmSurfaceElementForce = doc.createElement("SurfaceElementForce");
					elmMySurfaceForce.appendChild(elmSurfaceElementForce);
					
					pfs = ef.getPointForcesList().listIterator();
					while (pfs.hasNext()) {
						pf = (PointForce) pfs.next();
						
						//Creating DOM element "PointForce"
						org.w3c.dom.Element elmPointForce = doc.createElement("PointForce");
						elmSurfaceElementForce.appendChild(elmPointForce);
						
						//Creating DOM element "PointForceCoords"
						org.w3c.dom.Element elmPointForceCoords = doc.createElement("PointForceCoords");
						elmPointForce.appendChild(elmPointForceCoords);
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().x)));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().y)));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().z)));
						
						//Creating DOM element "PointForceLoad"
						org.w3c.dom.Element elmPointForceLoad = doc.createElement("PointForceLoad");
						elmPointForce.appendChild(elmPointForceLoad);
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(0))));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(1))));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(2))));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(3))));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(4))));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(5))));
						
					}
				
				}//End of creating DOM element SurfaceElementForce
				
				//Creating DOM element "MyVolumeForce"
				org.w3c.dom.Element elmMyVolumeForce = doc.createElement("MyVolumeForce");
				elmElement.appendChild(elmMyVolumeForce);
				
				//Creating DOM element "VolumeElementForce"
				efs = elm.getElmVolumeForcesList().listIterator();
				while (efs.hasNext()) {
					ef = (ElementForce)efs.next();
					org.w3c.dom.Element elmVolumeElementForce = doc.createElement("VolumeElementForce");
					elmMyVolumeForce.appendChild(elmVolumeElementForce);
					
					pfs = ef.getPointForcesList().listIterator();
					while (pfs.hasNext()) {
						pf = (PointForce) pfs.next();
						
						//Creating DOM element "PointForce"
						org.w3c.dom.Element elmPointForce = doc.createElement("PointForce");
						elmVolumeElementForce.appendChild(elmPointForce);
						
						//Creating DOM element "PointForceCoords"
						org.w3c.dom.Element elmPointForceCoords = doc.createElement("PointForceCoords");
						elmPointForce.appendChild(elmPointForceCoords);
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().x)));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().y)));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().z)));
						
						//Creating DOM element "PointForceLoad"
						org.w3c.dom.Element elmPointForceLoad = doc.createElement("PointForceLoad");
						elmPointForce.appendChild(elmPointForceLoad);
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(0))));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(1))));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(2))));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(3))));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(4))));
						elmPointForce.appendChild(doc.createTextNode(" "));
						elmPointForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(5))));
						
					}
				
				}//End of creating DOM element VolumeElementForce
				
				//Creating DOM element "ActionsAtExtremities"
				if (processed && !hypostatic && elmType==ELM_FRAME) {
					org.w3c.dom.Element elmActionsAtExtremities = doc.createElement("ActionsAtExtremities");
					fmt.applyPattern("####0.000");
					elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(((FrameElement)elm).getActionAtExtremity(0)))));
					elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
					elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(((FrameElement)elm).getActionAtExtremity(1)))));
					elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
					elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(((FrameElement)elm).getActionAtExtremity(2)))));
					elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
					elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(((FrameElement)elm).getActionAtExtremity(3)))));
					elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
					elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(((FrameElement)elm).getActionAtExtremity(4)))));
					elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
					elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(((FrameElement)elm).getActionAtExtremity(5)))));
					elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
					elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(((FrameElement)elm).getActionAtExtremity(6)))));
					elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
					elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(((FrameElement)elm).getActionAtExtremity(7)))));
					elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
					elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(((FrameElement)elm).getActionAtExtremity(8)))));
					elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
					elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(((FrameElement)elm).getActionAtExtremity(9)))));
					elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
					elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(((FrameElement)elm).getActionAtExtremity(10)))));
					elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
					elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(((FrameElement)elm).getActionAtExtremity(11)))));
					elmElement.appendChild(elmActionsAtExtremities);
				}
				
				//Creating DOM element "GaussPointsList"
				if (processed && !hypostatic && elmType==ELM_PARAMETRIC) {
					org.w3c.dom.Element elmGaussPointsList = doc.createElement("GaussPointList");
					
					//Creating DOM element "GaussPointResults"
					org.w3c.dom.Element elmGaussPointResults = doc.createElement("GaussPointResults");
					anl = elm.getAnalysisModel();
					elmGaussPointsList.appendChild(elmGaussPointResults);
					
					//Creating DOM element "GPCoords"
					org.w3c.dom.Element elmGPCoords = doc.createElement("GPCartesianCoords");
					elmGPCoords.appendChild(doc.createTextNode("x y z"));
					elmGaussPointResults.appendChild(elmGPCoords);
					
					//Creating DOM element "GPStrains"
					org.w3c.dom.Element elmGPStrains = doc.createElement("GPStrains");
					elmGPStrains.appendChild(doc.createTextNode(anl.getStrainLabels()));
					elmGaussPointResults.appendChild(elmGPStrains);
					
					//Creating DOM element "GPStresses"
					org.w3c.dom.Element elmGPStresses = doc.createElement("GPStresses");
					elmGPStresses.appendChild(doc.createTextNode(anl.getStressLabels()));
					elmGaussPointResults.appendChild(elmGPStresses);
					
					//Creating DOM elements "GaussPoint"
					ListIterator ips = ((ParametricElement)elm).getIntegrationPointsList().listIterator();
					mat = elm.getMaterial();
					while(ips.hasNext()) {
						ip = (IntegrationPoint) ips.next();
						
						org.w3c.dom.Element elmGaussPoint = doc.createElement("GaussPoint");
						elmGaussPoint.setAttribute("label", "" + ip.getLabel());
						elmGaussPointsList.appendChild(elmGaussPoint);
						
						//Creating DOM elements "GPCartesianCoord"
						org.w3c.dom.Element elmGPCoord = doc.createElement("GPCartesianCoord");
						fmt.applyPattern("0.0000E00");
						Shape shp = elm.getShape();
						IVector Ni = shp.getShapeFunction(ip.getCoords());
						int nNos = elm.getIncidence().size();
						IVector xi = new IVector(nNos);
						IVector yi = new IVector(nNos);
						IVector zi = new IVector(nNos);
						ListIterator elmNodes = elm.getIncidence().listIterator();
						int j = 0;
						//double prov[] = ip.getCoords();
						
						while (elmNodes.hasNext()) {
							node = (Node)elmNodes.next();
							xi.setElement(j,(node.getCoord()).x);
							yi.setElement(j,(node.getCoord()).y);
							zi.setElement(j,(node.getCoord()).z);
							j++;
						}
						
						elmGPCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(Ni.dot(xi)))));
						elmGPCoord.appendChild(doc.createTextNode(" "));
						elmGPCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(Ni.dot(yi)))));
						elmGPCoord.appendChild(doc.createTextNode(" "));
						elmGPCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(Ni.dot(zi)))));
						elmGaussPoint.appendChild(elmGPCoord);
						
						//Creating DOM elements "GPStrain"
						org.w3c.dom.Element elmGPStrain = doc.createElement("GPStrain");
						IVector strains = anl.getStrainVector(((ParametricElement)elm).getPointStrains(ip.getCoords()), mat);
						fmt.applyPattern("0.000E00");
						for(int m = 0; m < (strains.getSize()); m++) {
							elmGPStrain.appendChild(doc.createTextNode(String.valueOf(fmt.format(strains.getElement(m)))));
							if(m < (strains.getSize()-1))//para retirar o espaco no final da tag
							elmGPStrain.appendChild(doc.createTextNode(" "));
						}
						elmGaussPoint.appendChild(elmGPStrain);
						
						//Creating DOM elements "GPStress"
						org.w3c.dom.Element elmGPStress = doc.createElement("GPStress");
						IVector stresses = anl.getStressVector(((ParametricElement)elm).getPointStress(ip.getCoords()), mat);
						fmt.applyPattern("0.000000E00");
						for(int k = 0; k < (stresses.getSize()); k++) {
							elmGPStress.appendChild(doc.createTextNode(String.valueOf(fmt.format(stresses.getElement(k)))));
							if(k < (stresses.getSize()-1))//para retirar o espaco no final da tag
							elmGPStress.appendChild(doc.createTextNode(" "));
						}
						elmGaussPoint.appendChild(elmGPStress);
					}
					elmElement.appendChild(elmGaussPointsList);
					
					//Creating DOM element "NodalStressesAndStrains"
					org.w3c.dom.Element elmNodalResults = doc.createElement("NodalStressesAndStrains");
					anl = elm.getAnalysisModel();
					IMatrix stressesAtNode = ((ParametricElement)elm).getNodalPointStress();
					IMatrix strainsAtNode = ((ParametricElement)elm).getNodalPointStrains();
					IVector aux = new IVector(stressesAtNode.getNumRow());
					IVector aux1 = new IVector(strainsAtNode.getNumRow());
					int m = 0;//counter used in nodal deforms
					int n = 0;//counter used in nodal stresses
					
					//Creating DOM element "NodeResults"
					org.w3c.dom.Element elmNodeResults = doc.createElement("NodeResults");
					elmNodalResults.appendChild(elmNodeResults);
					
					//Creating DOM element "NodeCoords"
					org.w3c.dom.Element elmNodeCoords = doc.createElement("NodeCoords");
					elmNodeCoords.appendChild(doc.createTextNode("x y z"));
					elmNodeResults.appendChild(elmNodeCoords);
					
					//Creating DOM element "NodeStrains"
					org.w3c.dom.Element elmNodeStrains = doc.createElement("NodeStrains");
					elmNodeStrains.appendChild(doc.createTextNode(anl.getStrainLabels()));
					elmNodeResults.appendChild(elmNodeStrains);
					
					//Creating DOM element "NodeStresses"
					org.w3c.dom.Element elmNodeStresses = doc.createElement("NodeStresses");
					elmNodeStresses.appendChild(doc.createTextNode(anl.getStressLabels()));
					elmNodeResults.appendChild(elmNodeStresses);
					
					ListIterator nds = elm.getIncidence().listIterator();
					while(nds.hasNext()) {
						node = (Node) nds.next();
						
						//Creating DOM elements "ElmNode"
						org.w3c.dom.Element elmNodeLabel = doc.createElement("ElmNode");
						elmNodeLabel.setAttribute("label", "" + node.getLabel());
						elmNodalResults.appendChild(elmNodeLabel);
						
						//Creating DOM elements "NodeCoord"
						org.w3c.dom.Element elmNodeCoord = doc.createElement("NodeCoord");
						fmt.applyPattern("0.000E00");
						elmNodeCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getCoord().x))));
						elmNodeCoord.appendChild(doc.createTextNode(" "));
						elmNodeCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getCoord().y))));
						elmNodeCoord.appendChild(doc.createTextNode(" "));
						elmNodeCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getCoord().z))));
						elmNodeLabel.appendChild(elmNodeCoord);
						
						//Creating DOM elements "NodeStrain"
						org.w3c.dom.Element elmNodeStrain = doc.createElement("NodeStrain");
						fmt.applyPattern("0.000E00");
						strainsAtNode.getColumn(m, aux1);
						for (int p = 0; p < (aux1.getSize()); p++) {
							elmNodeStrain.appendChild(doc.createTextNode(String.valueOf(fmt.format(aux1.getElement(p)))));
							if(p < (aux1.getSize()-1))//to delete the blank space at the end of tag
							elmNodeStrain.appendChild(doc.createTextNode(" "));
						}
						m++;
						elmNodeLabel.appendChild(elmNodeStrain);
						
						//Creating DOM elements "NodeStress"
						org.w3c.dom.Element elmNodeStress = doc.createElement("NodeStress");
						fmt.applyPattern("0.000E00");
						stressesAtNode.getColumn(n, aux);
						for (int k = 0; k < (aux.getSize()); k++) {
							elmNodeStress.appendChild(doc.createTextNode(String.valueOf(fmt.format(aux.getElement(k)))));
							if(k < (aux.getSize()-1))//to delete the blank space at the end of tag
							elmNodeStress.appendChild(doc.createTextNode(" "));
						}
						n++;
						elmNodeLabel.appendChild(elmNodeStress);
					}
				elmElement.appendChild(elmNodalResults);
				}
			
			}//End of creating DOM elements "Element"
			
			//Creating DOM element "Message"
			if (hypostatic) {
				org.w3c.dom.Element elmMessage = doc.createElement("Message");
				elmMessage.appendChild(doc.createTextNode("Hypostatic Structure"));
				elmModel.appendChild(elmMessage);
			}//End of creating DOM elements "Message"
			
			docOutput = doc;
			
		} catch (ParserConfigurationException pce) {
			parent.addOutputText("Could not create DOM parser.");
			parent.addOutputText(pce.getMessage());
		}
		
	}
	
//*****************************************************************************
	
	/** Serializes the DOM tree into a XML file.
	*@param resultFile The name of the output file.
	*/
	public void serializeXMLDocument(File resultFile) {
		try {
			Transformer trans = TransformerFactory.newInstance().newTransformer();
			trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "InsaneData.dtd");
			trans.setOutputProperty(OutputKeys.INDENT, "yes");
			trans.setOutputProperty(OutputKeys.ENCODING, "ISO8859-1");
			trans.transform(new DOMSource((Document) docOutput ), new StreamResult(resultFile));
			parent.addOutputText("File \"" + resultFile.getPath() + "\" successfully exported.");
		} catch (TransformerException te) {
			parent.addOutputText("Problem serializing DOM tree");
			parent.addOutputText(te.getMessageAndLocation());
		}
	}
	
//*****************************************************************************
}
